package com.heyun.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heyun.bean.HeyunEmp;
import com.heyun.bean.HeyunOppoManage;
import com.heyun.service.HeyunEmpService;
import com.heyun.service.HeyunOppoManageService;

/**
 * @author 成栋梁
 *
 */
@RequestMapping("/oppo")
@Controller
public class HeyunOppoManageController {
	@Autowired
	private HeyunOppoManageService hyoms;
	@Autowired
	private HeyunEmpService hyes;

	/**
	 * @author 成栋梁 查询所有的机会
	 */
	@RequestMapping("getAllOppo")
	@ResponseBody
	public Map getAllOppo(String curr) {
		Map map = new HashMap();
		int pageNum = Integer.valueOf(curr);
		Page page = PageHelper.startPage(pageNum, 2);
		List<HeyunOppoManage> list = hyoms.listAllOppo();
		int totalPages = page.getPages();
		map.put("oppoList", list);
		map.put("totalPages", totalPages);
		return map;
	}

	/**
	 * @author 成栋梁 添加保存机会
	 */
	@RequestMapping("saveOppo")
	@ResponseBody
	public Map saveOppo(HeyunOppoManage hyom, HttpServletRequest request) {
		Map map = new HashMap();
		HeyunEmp user = (HeyunEmp) request.getSession().getAttribute("user");
		int empId = user.getEmpId();
		hyom.setEmpId(empId);
		hyom.setOwnerId(1);
		int a = hyoms.insertOppoManage(hyom);
		if (a > 0) {
			map.put("message", "添加成功");
		} else {
			map.put("message", "系统出错，请重新操作");
		}
		return map;
	}

	/**
	 * @author 成栋梁 获取机会内容
	 */
	@RequestMapping("getOppoMessage")
	@ResponseBody
	public Map getOppoMessage(String oppoId) {
		Map map = new HashMap();
		int oppoManageId = Integer.valueOf(oppoId);
		HeyunOppoManage oppo = hyoms.getOppo(oppoManageId);
		map.put("oppo", oppo);
		return map;
	}

	/**
	 * @author 成栋梁 更新机会内容
	 */
	@RequestMapping("updateOppo")
	@ResponseBody
	public Map updateOppo(HeyunOppoManage hyom) {
		Map map = new HashMap();
		int a = hyoms.updateOppo(hyom);
		if (a > 0) {
			map.put("message", "修改成功");
		} else {
			map.put("message", "系统出错，请重新操作");
		}
		return map;
	}

	/**
	 * @author 成栋梁 删除机会
	 */
	@RequestMapping("deleteOppo")
	@ResponseBody
	public Map deleteOppo(String oppoId) {
		Map map = new HashMap();
		int a = hyoms.removeOppo(Integer.valueOf(oppoId));
		if (a > 0) {
			map.put("message", "修改成功");
		} else {
			map.put("message", "系统出错，请重新操作");
		}
		return map;
	}

	/**
	 * @author 成栋梁 获取机会和所有员工信息
	 */
	@RequestMapping("giveOppoEmp")
	@ResponseBody
	public Map giveOppoEmp(String oppoId, String curr) {
		Map map = new HashMap();
		Page page = PageHelper.startPage(Integer.valueOf(curr), 2);
		List<HeyunEmp> listEmp = hyes.listEmp();
		int totals = (int) page.getPages();
		map.put("empList", listEmp);
		map.put("totalPages", totals);
		HeyunOppoManage oppo = hyoms.getOppo(Integer.valueOf(oppoId));
		map.put("oppo", oppo);
		return map;
	}

	/**
	 * @author 成栋梁 更改机会拥有者
	 */
	@RequestMapping("giveOppo")
	@ResponseBody
	public Map giveOppo(String oppoId, String empId) {
		System.out.println("********oppoId=" + oppoId + "********empId="
				+ empId);
		Map map = new HashMap();
		Map map1 = new HashMap();
		map1.put("oppoManageId", oppoId);
		map1.put("ownerId", empId);
		int a = hyoms.updateOppoOwner(map1);
		if (a > 0) {
			map.put("message", "机会分配成功");
		} else {
			map.put("message", "系统出错，请重新操作");
		}
		return map;
	}

	/**
	 * @author 成栋梁 
	 * 多条件查询机会
	 */
	@RequestMapping("searchOppo")
	@ResponseBody
	public Map searchOppo(String owner, String oppoName, String curr) {
		Map map = new HashMap();
		Map map1 = new HashMap();
		int a = Integer.valueOf(owner);
		if (a != 1) {
			map1.put("ownerId", 2);
		} else {
			map1.put("ownerId", 1);
		}
		if (oppoName != null && oppoName != "") {
			map1.put("oppoName", oppoName);
		}
		Page page = PageHelper.startPage(Integer.valueOf(curr), 2);
		List<HeyunOppoManage> oppoList = hyoms.searchOppo(map1);
		int totals = (int) page.getPages();
		map.put("oppoList", oppoList);
		map.put("totalPages", totals);
		return map;
	}
}
